package com.minivision.sdktestdemo.controler.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.event.DataModificationEvent;
import com.minivision.sdktestdemo.event.PlayMusicEvent;
import com.minivision.sdktestdemo.service.LockService;
import com.minivision.sdktestdemo.service.MusicService;
import com.minivision.sdktestdemo.ui.MusicControlHandler;
import com.minivision.sdktestdemo.ui.MusicPlayActivity;
import com.minivision.sdktestdemo.ui.view.MusicListPopContentView;
import com.minivision.sdktestdemo.ui.view.PopupView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by devtang on 2016/4/27.
 */
public class MusicMiniPlayControler {
    private Activity mContext;
    private PlayMusicEvent mPlayMusicEvent;
    private MusicControlHandler.onProgressUpdated mProgressListener;
    private PlayListGetter mPlayListGetter;
    private PopupView mPopupWindow;

    public MusicMiniPlayControler(Activity activity) {
        ButterKnife.bind(this, activity);
        mContext = activity;
        mProgressListener = (MusicControlHandler.onProgressUpdated) activity;
        mPlayListGetter = (PlayListGetter) activity;
    }

    @Bind(R.id.fl_control)
    FrameLayout mControlView;

    @Bind(R.id.btn_play)
    ImageButton mBtnPlay;

    @Bind(R.id.tv_name)
    TextView mTvName;

    @Bind(R.id.tv_subname)
    TextView mTvSubName;

    @Bind(R.id.progressDeterminate)
    ProgressBar mProgressBar;

    @Bind(R.id.img_album)
    SimpleDraweeView mCoverView;

    @OnClick(R.id.btn_play)
    void onPlay() {
        switch (mPlayMusicEvent.mStatus) {
            case PlayMusicEvent.STOP:
            case PlayMusicEvent.PAUSE:
                mPlayMusicEvent.mStatus = PlayMusicEvent.PLAY;
                mContext.startService(new Intent(mContext, MusicService.class));
                mProgressListener.startUpdate();
                break;
            case PlayMusicEvent.PLAY:
                mPlayMusicEvent.mStatus = PlayMusicEvent.PAUSE;
                break;
        }
        EventBus.getDefault().post(mPlayMusicEvent);
    }

    @OnClick(R.id.btn_play_next)
    void onPlayNext() {
        mPlayMusicEvent.mStatus = PlayMusicEvent.NEXT;
        EventBus.getDefault().post(mPlayMusicEvent);
    }

    //每次收到广播的时候,会被父activity调用
    public void updateView(PlayMusicEvent event) {
        if (!event.mUri.isEmpty()) {
            switch (event.mStatus) {
                case PlayMusicEvent.PLAY:
                    mBtnPlay.setImageResource(R.drawable.playbar_btn_pause);
                    mContext.startService(new Intent(mContext, LockService.class));
                    break;
                case PlayMusicEvent.STOP:
                    update(event, 0, event.mDuration);
                case PlayMusicEvent.PAUSE:
                    mBtnPlay.setImageResource(R.drawable.playbar_btn_play);
                    break;
                default:
                    return;
            }
            if (mPlayMusicEvent == null) {
                mPlayMusicEvent = event;
            } else {
                mPlayMusicEvent.setPlayEvent(event);
            }
            mTvName.setText(event.mName);
            mTvName.setSelected(true);
            mCoverView.setImageURI(event.mAlbumUri);
            mTvSubName.setText(event.mSubName);
            if (mControlView.getVisibility() == View.GONE) {
                mControlView.setVisibility(View.VISIBLE);
            }
        } else {
            mControlView.setVisibility(View.GONE);
        }
    }

    public void updateView(DataModificationEvent event) {
        //运行在子线程里
        switch (event.mAction) {
            case DataModificationEvent.ACTION_CHANGE:
                if (mPlayMusicEvent != null && event.mBeans.get(0).getmPath().equals(mPlayMusicEvent.mUri)) {
                    mPlayMusicEvent.mName = event.mBeans.get(0).getmMusicName();
                    mPlayMusicEvent.mSubName = event.mBeans.get(0).getmSingerName();
                    mContext.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            updateView(mPlayMusicEvent);
                        }
                    });
                }
                break;
            case DataModificationEvent.ACTION_LIST_CLEAR:
                mContext.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mControlView.setVisibility(View.GONE);
                    }
                });
                break;
        }
    }

    @OnClick(R.id.fl_control)
    public void checkDetail() {
        Intent intent = new Intent(mContext, MusicPlayActivity.class);
        mContext.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        mContext.startActivityForResult(intent, 1);
    }

    @OnClick(R.id.btn_list)
    public void onClickList() {
        if (mPlayListGetter.getPlayList() != null) {
            if (mPopupWindow == null) {
                mPopupWindow = new PopupView(mContext, new MusicListPopContentView(mContext));
            }
            mPopupWindow.show(mPlayListGetter.getPlayList(), mPlayMusicEvent);
        }
    }

    public interface PlayListGetter {
        List<MusicBean> getPlayList();
    }

    public void dismiss() {
        mControlView.setVisibility(View.GONE);
    }

    public void init(PlayMusicEvent event) {
        if (event == null || event.mUri == null || event.mUri.isEmpty()) {
            //如果没有播放的歌曲,则不显示
            mControlView.setVisibility(View.GONE);
        } else {
            //同步音乐状态
            if (mPlayMusicEvent == null) {
                mPlayMusicEvent = event;
            } else {
                mPlayMusicEvent.setPlayEvent(event);
            }
            //设定需要显示的音乐状态
            updateView(mPlayMusicEvent);
            mControlView.setVisibility(View.VISIBLE);
        }
    }

    public void update(PlayMusicEvent event, int value, int max) {
        if (event.mStatus == PlayMusicEvent.STOP) {
            value = 0;
        }
        mProgressBar.setMax(max);
        mProgressBar.setProgress(value);
    }

    public void update() {
        if (mPlayMusicEvent != null) {
            updateView(mPlayMusicEvent);
            update(mPlayMusicEvent, mPlayMusicEvent.mCurPos, mPlayMusicEvent.mDuration);
        }
    }
}
